package com.ifast.api.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ifast.api.config.LoginConfig;
import com.ifast.common.domain.LogDO;
import com.ifast.common.service.LogService;
import com.ifast.common.utils.HttpContextUtils;
import com.ifast.common.utils.IPUtils;
import com.ifast.sys.dao.UserDao;
import com.ifast.sys.domain.UserDO;
import com.ifast.sys.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationListener;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;


public class LoginListen implements AuthenticationListener {

    UserService userService;
    LogService logService;
    public LoginListen(UserService userService,LogService logService){

        this.userService = userService;
        this.logService = logService;
    }
    @Override
    public void onSuccess(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {

        String userDOString = authenticationInfo.getPrincipals().toString()
                .replace("[", "{\"").replace("]", "\"}").replaceAll(" ","")
                .replace("UserDO", "").replaceAll("=", "\":\"").replaceAll(",", "\",\"");
        long id = Long.valueOf(JSON.parseObject(userDOString).get("id").toString());
        UserDO userDO = userService.selectById(id);
        userDO.setLoginNum(0);
        userService.updateById(userDO);
        //System.out.println(userDO);
    }

    @Override
    public void onFailure(AuthenticationToken authenticationToken, AuthenticationException e) {
        String username = authenticationToken.getPrincipal().toString();
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("username",username);
        UserDO userDO = userService.selectOne(wrapper);
        if (userDO.getLoginNum()>= LoginConfig.loginNum){
            userDO.setLocked(true);
            userService.updateById(userDO);
            //添加一条锁定  记录
            LogDO logDO = new LogDO();
            logDO.setIssucceed(false);
            logDO.setUsername(username);
            logDO.setOperation("锁定用户");
            logDO.setMethod("POST /api/user/login");
            logDO.setParams("{}");
            HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
            logDO.setIp(IPUtils.getIpAddr(request));
            logDO.setGmtCreate(new Date());
            logService.insert(logDO);

        }else {
            userDO.setLoginNum(userDO.getLoginNum()+1);
            userService.updateById(userDO);
            LogDO logDO = new LogDO();
            logDO.setIssucceed(false);
            logDO.setUsername(username);
            logDO.setOperation("登录");
            logDO.setMethod("POST /api/user/login");
            logDO.setParams("{}");
            HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
            logDO.setIp(IPUtils.getIpAddr(request));
            logDO.setGmtCreate(new Date());
            logService.insert(logDO);
        }
    }

    @Override
    public void onLogout(PrincipalCollection principalCollection) {
        System.out.println();
    }
}
